<html>
<head>
  <link rel="stylesheet" type="text/css" href="./styles1/tabber.css">
<script type="text/javascript" src="./js/tabber.js"></script>
  
</head>
<body>

<?php
$username = "fossilplot";
$password = "fossilplot";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");


//select a database to work with
$selected = mysql_select_db("fossilplot",$dbhandle)
  or die("Could not select examples");

$debug=0;
$fad=$_POST['fad'];
$lad=$_POST['lad'];

$d=$_POST['diversity'];
$o=$_POST['Origination'];
$e=$_POST['Extinction'];
$radio_d=$_POST['Div1'];

$radio_o=$_POST['Ori1'];

$radio_e=$_POST['Ex1'];

$arrfad=explode(',',$fad);
$arrlad=explode(',',$lad);

//echo "fad is $fad<br><br>";
//echo "lad is $lad<br><br>";

echo "d is $d"."<br>";
echo "o is $o"."<br>";
echo "e is $e"."<br>";

echo "radio_d is $radio_d"."<br>";
echo "radio_o is $radio_o"."<br>";
echo "radio_e is $radio_e"."<br>";


//for ($i=0; $i<sizeof($arrfad);$i++){
 // echo $arrfad[$i]."<br>";

//}


$result_stgintv=mysql_query("select * from stgintv") or die ("error selecting from stgintv!!");
for($tmp=0; $tmp<mysql_num_rows($result_stgintv); $tmp++ ){
  $arr[$tmp]=0;
  $arrdvt[$tmp] =0;
  $arrext[$tmp]=0;
 }
 
 for ($i=0; $i<sizeof($arrfad);$i++){
    while($row_stgintv=mysql_fetch_array($result_stgintv)){
      if ($row_stgintv{'end'}<=$arrfad[$i] && $arrfad[$i]<= $row_stgintv{'begin'}){ //this if for origination
          $arr[$row_stgintv{'id'}]+=1;
         // break;
      }
      
      if($arrlad[$i] <= $row_stgintv{'begin'} && $arrlad[$i] >= $row_stgintv{'end'}){
        $arrext[$row_stgintv{'id'}] += 1;
      }
      
      if($arrfad[$i] <= $row_stgintv{'begin'} && $arrlad[$i] >= $row_stgintv{'end'}){
        $arrdvt[$row_stgintv{'id'}] += 1;
        //break;
      }
      else if($arrfad[$i] <= $row_stgintv{'begin'} && $arrfad[$i] >= $row_stgintv{'end'}){
        $arrdvt[$row_stgintv{'id'}] += 1;
        //break;
      }
      else if($arrlad[$i] <= $row_stgintv{'begin'} && $arrlad[$i] >= $row_stgintv{'end'}){
        $arrdvt[$row_stgintv{'id'}] += 1;
        //break;
      }
      else if($arrfad[$i] >= $row_stgintv{'begin'} && $arrlad[$i] <= $row_stgintv{'end'}){
        $arrdvt[$row_stgintv{'id'}] += 1;
        //break;
      }
      
      
  
  
    } 
    $result_stgintv=mysql_query("select * from stgintv") or die ("error!!"); 
 }
 
 
 if($debug){
 $total=0;
 for($tmp=0; $tmp<mysql_num_rows($result_stgintv); $tmp++ ){
  print "arr is ".$arr[$tmp]."<br>";
   $total = $total+ $arr[$tmp];
 }
 echo "total is ".$total;
 
 } //debug
 
 
 if($o=="on"){
 
 
 $File = "../../../admin1/list/graph/write.php";
$Handle = fopen($File, 'w');
$Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
\$datay=array";
fwrite($Handle, $Data);

$barval="($arr[0]";

if($radio_o=="Rate2"){
  for($tmp=1; $tmp<sizeof($arr); $tmp++){
    $rate=($arr[$tmp]/sizeof($arrfad))*100;
    $barval =$barval.",".$rate;
  }
}

else{
  for($tmp=1; $tmp<sizeof($arr); $tmp++){
    
    $barval =$barval.",".$arr[$tmp];
  }

}

$barval=$barval.")";

if($debug) {echo "barvalue is".$barval;}

$Data="$barval;

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();

\$datax = array(\"V\",\"Cm(l)\",\"Cm(m)\",\"Cm(u)\",\"O(l)\",\"O(m)\",\"O(u)\",\"S(l)\",\"S(m)\",\"S(u)\",\"D(l)\",\"D(m)\",\"D(u)\",\"CMs\",\"CPn\",\"P(l)\",\"P(u)\",\"Tr(l)\",\"Tr(m)\",\"Tr(u)\",\"J(l)\",\"J(m)\",\"J(u)\",\"K(l)\",\"K(u)\",\"Pg(Pal)\",\"Pg(Eo)\",\"Pg(Oli)\",\"Ng(Mio)\",\"Ng(Pli)\",\"Ng(Plei)\",\"Ng(Re)\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);

\$graph->img->SetMargin(40,30,20,80);
\$bplot = new BarPlot(\$datay);
\$bplot->SetFillColor('green');
\$bplot->value->Show();
\$graph->Add(\$bplot);
\$graph->title->Set(\"Origination\");
\$graph->xaxis->title->Set(\"Stage Interval\");
\$graph->xaxis->title->SetMargin(40);
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->title->SetFont(FF_FONT1,FS_BOLD,8);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->Stroke();
?>";

  fwrite($Handle, $Data);
  fclose($Handle);

  //unlink the old graph
  $myFile = "../../../admin1/list/graph/jpgraph_cache/write.png";
  unlink($myFile);


  //echo "<img src=\"http://localhost/test/admin1/list/graph/write.php\">";
  
  } //end of if($o is on)
  
  
  
if($d=="on"){
 
 
 $File = "../../../admin1/list/graph/writedvt.php";
 $Handle = fopen($File, 'w');
 $Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
\$datay=array";
fwrite($Handle, $Data);

$barval="($arrdvt[0]";
if($radio_d =="rate"){
  $total=0;
  for($tmp=1; $tmp<sizeof($arrdvt); $tmp++){
      $total += $arrdvt[$tmp];
  }
  
  for($tmp=1; $tmp<sizeof($arrdvt); $tmp++){
    $rate=($arrdvt[$tmp]/$total)*100;
    $barval =$barval.",".$rate;
  }  
 }
 
 else{
  for($tmp=1; $tmp<sizeof($arrdvt); $tmp++){
    
    $barval =$barval.",".$arrdvt[$tmp];
  }

}

$barval=$barval.")";

if($debug) {echo "barvalue is".$barval;}

$Data="$barval;

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();

\$datax = array(\"V\",\"Cm(l)\",\"Cm(m)\",\"Cm(u)\",\"O(l)\",\"O(m)\",\"O(u)\",\"S(l)\",\"S(m)\",\"S(u)\",\"D(l)\",\"D(m)\",\"D(u)\",\"CMs\",\"CPn\",\"P(l)\",\"P(u)\",\"Tr(l)\",\"Tr(m)\",\"Tr(u)\",\"J(l)\",\"J(m)\",\"J(u)\",\"K(l)\",\"K(u)\",\"Pg(Pal)\",\"Pg(Eo)\",\"Pg(Oli)\",\"Ng(Mio)\",\"Ng(Pli)\",\"Ng(Plei)\",\"Ng(Re)\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);

\$graph->img->SetMargin(40,30,20,80);
\$bplot = new BarPlot(\$datay);
\$bplot->SetFillColor('blue');
\$bplot->value->Show();
\$graph->Add(\$bplot);
\$graph->title->Set(\"Generic Diversity\");
\$graph->xaxis->title->Set(\"Stage Interval\");
\$graph->xaxis->title->SetMargin(40);
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->title->SetFont(FF_FONT1,FS_BOLD,8);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->Stroke();
?>";

  fwrite($Handle, $Data);
  fclose($Handle);

  //unlink the old graph
  
  $myFile = "../../../admin1/list/graph/jpgraph_cache/writedvt.png";
  unlink($myFile);


  //echo "<img src=\"http://localhost/test/admin1/list/graph/writedvt.php\">";
  
  } //end of if($d is on)
  
  
  
  
  
  if($e=="on"){
 
 
 $File = "../../../admin1/list/graph/writeext.php";
  $Handle = fopen($File, 'w');
  $Data = "<?php
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_bar.php\");
\$datay=array";
fwrite($Handle, $Data);

$barval="($arrext[0]";

if($radio_e =="Rate3"){
  $total=0;
  for($tmp=1; $tmp<sizeof($arrext); $tmp++){
      $total += $arrext[$tmp];
  }
  
  for($tmp=1; $tmp<sizeof($arrext); $tmp++){
    $rate=($arrext[$tmp]/$total)*100;
    $barval =$barval.",".$rate;
  }  
 }
 
 else{
  for($tmp=1; $tmp<sizeof($arrext); $tmp++){
    
    $barval =$barval.",".$arrext[$tmp];
  }

}



$barval=$barval.")";

if($debug) {echo "barvalue is".$barval;}

$Data="$barval;

\$graph = new Graph(800,450,\"auto\");    
\$graph->SetScale(\"textlin\");
\$graph->SetShadow();

\$datax = array(\"V\",\"Cm(l)\",\"Cm(m)\",\"Cm(u)\",\"O(l)\",\"O(m)\",\"O(u)\",\"S(l)\",\"S(m)\",\"S(u)\",\"D(l)\",\"D(m)\",\"D(u)\",\"CMs\",\"CPn\",\"P(l)\",\"P(u)\",\"Tr(l)\",\"Tr(m)\",\"Tr(u)\",\"J(l)\",\"J(m)\",\"J(u)\",\"K(l)\",\"K(u)\",\"Pg(Pal)\",\"Pg(Eo)\",\"Pg(Oli)\",\"Ng(Mio)\",\"Ng(Pli)\",\"Ng(Plei)\",\"Ng(Re)\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);

\$graph->img->SetMargin(40,30,20,80);
\$bplot = new BarPlot(\$datay);
\$bplot->SetFillColor('orange');
\$bplot->value->Show();
\$graph->Add(\$bplot);
\$graph->title->Set(\"Extinction\");
\$graph->xaxis->title->Set(\"Stage Interval\");
\$graph->xaxis->title->SetMargin(40);
\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->title->SetFont(FF_FONT1,FS_BOLD,8);
\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
\$graph->Stroke();
?>";

  fwrite($Handle, $Data);
  fclose($Handle);

  //unlink the old graph
    
  $myFile = "../../../admin1/list/graph/jpgraph_cache/writeext.png";
  unlink($myFile);


 // echo "<img src=\"http://localhost/test/admin1/list/graph/writeext.php\">";
  
  } //end of if($e is on)
  
  
  
  if($d== "on"){
    if($o=="on"){
      if($e=="on"){
        echo "<div class=\"tabber\" id=\"tab1\">";          

          echo "<div class=\"tabbertab\" title=\"Diversity\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writedvt.php\">";
          echo "</div>";
          
          echo "<div class=\"tabbertab\" title=\"Origination\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/write.php\">"; 
          echo "</div>";


          echo "<div class=\"tabbertab\" title=\"Extinction\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writeext.php\">";
          echo "</div>";
        echo "</div>";
      
      }
      else { // e is not on, o and d are on    
        echo "<div class=\"tabber\" id=\"tab1\">";          

          echo "<div class=\"tabbertab\" title=\"Diversity\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writedvt.php\">";
          echo "</div>";
          
          echo "<div class=\"tabbertab\" title=\"Origination\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/write.php\">"; 
          echo "</div>";          
        echo "</div>";      
      }    
    }
    else{ //o is not on, d is on, e is on or not
      if($e=="on"){ //d is on and e is on, o not on
        echo "<div class=\"tabber\" id=\"tab1\">";          

          echo "<div class=\"tabbertab\" title=\"Diversity\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writedvt.php\">";
          echo "</div>";
          
          echo "<div class=\"tabbertab\" title=\"Extinction\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writeext.php\">";
          echo "</div>";
        echo "</div>";     
      
      }
      else{ //e and o are not on
        echo "<div class=\"tabber\" id=\"tab1\">";          

          echo "<div class=\"tabbertab\" title=\"Diversity\">";
          echo "<img src=\"http://localhost/test/admin1/list/graph/writedvt.php\">";
          echo "</div>";          
          
        echo "</div>";      
      
      }    
    }// for 'else{ //o is not on, d is on, e is on or not'
  
  }
  else{ //$d is not on
    if($o=="on"){
      if($e=="on"){
          echo "<div class=\"tabber\" id=\"tab1\">";  
            echo "<div class=\"tabbertab\" title=\"Origination\">";
            echo "<img src=\"http://localhost/test/admin1/list/graph/write.php\">"; 
            echo "</div>";  
            echo "<div class=\"tabbertab\" title=\"Extinction\">";
            echo "<img src=\"http://localhost/test/admin1/list/graph/writeext.php\">";
            echo "</div>";
          echo "</div>";
      
      }else{ //only o is on
          echo "<div class=\"tabber\" id=\"tab1\">";  
            echo "<div class=\"tabbertab\" title=\"Origination\">";
            echo "<img src=\"http://localhost/test/admin1/list/graph/write.php\">"; 
            echo "</div>";            
          echo "</div>";
      
      }    
    
    }
    else { // d and o are off
      if ($e =="on"){
        echo "<div class=\"tabber\" id=\"tab1\">";  
            echo "<div class=\"tabbertab\" title=\"Extinction\">";
            echo "<img src=\"http://localhost/test/admin1/list/graph/writeext.php\">";
            echo "</div>";
          echo "</div>";
      
      }
      else{ //d,o,x are off
        echo "please select dox for the graph.";
      
      }
    
    
    }
  
  
  }
  
  
  
  


  
  



?>
 </body>

</html>